package stardust.product.platform.persistence.sql;


import stardust.product.platform.persistence.JdbcHelper;
import stardust.product.platform.persistence.PersistenceException;


public class SQL92Factory implements SQLFactory
{
	
	public String getName()
	{
		return "SQL";
	}

	public String getVersion()
	{
		return "92";
	}

	public String beginTransaction(String name) throws PersistenceException
	{
		if (name == null || name.isEmpty())
		{
			throw new PersistenceException("事务开启失败，无法设定匿名保存点。");
		}
		
		return "savepoint " + name;
	}
	
	public String rollbackTransaction(String name) throws PersistenceException
	{
		if (name != null && !name.isEmpty())
		{
			return "rollback to savepoint " + name;
		}
		
		return "rollback";
	}

	public String commitTransaction(String name) throws PersistenceException
	{
		return "commit";
	}

  public String insert(String tableName, String[] column) throws PersistenceException
  {
    StringBuffer buffer = new StringBuffer();
    
    buffer.append("insert into ").append(tableName).append(" (");
    
    for (int i = 0; i < column.length; i++)
    {
      buffer.append(column[i]);
      
      if (i < column.length - 1)
      {
        buffer.append(", ");
      }
    }
    
    buffer.append(") values (");
    
    for (int i = 0; i < column.length; i++)
    {
      buffer.append("?");
      
      if (i < column.length - 1)
      {
        buffer.append(", ");
      }
    }
    
    buffer.append(")");
    
    return buffer.toString();
  }

  public String update(String tableName, String[] column, String primaryKey) throws PersistenceException
  {
    StringBuffer buffer = new StringBuffer();
    
    buffer.append("update ").append(tableName).append(" set ");
    
    for (int i = 0; i < column.length; i++)
    {
      if (!column[i].equalsIgnoreCase(primaryKey))
      {
        buffer.append(column[i]).append(" = ?");
        
        if (i < column.length - 1)
        {
          buffer.append(", ");
        }
      }
    }
    
    buffer.append(" where ").append(primaryKey).append(" = ?");
    
    return buffer.toString();
  }
  
  public String delete(String tableName, String primaryKey) throws PersistenceException
  {
    StringBuffer buffer = new StringBuffer();
    
    buffer.append("delete from ").append(tableName).append(" where ").append(primaryKey).append(" = ?");
    
    return buffer.toString();
  }
  
  public String select(String tableName, String column, String primaryKey, int argc) throws PersistenceException
  {
    StringBuffer buffer = new StringBuffer();
    
    buffer.append("select ").append(column).append(" from ").append(tableName).append(" where ").append(primaryKey).append(" in (");
    
    for (int i = 0; i < argc; i++)
    {
      buffer.append("?");
      
      if (i < argc - 1)
      {
        buffer.append(", ");
      }
    }
    
    buffer.append(")");
    
    return buffer.toString();
  }

  public Long lastInsertId(JdbcHelper jdbcHelper, String sequence) throws PersistenceException
  {
    throw new IllegalStateException("not support yet.");
  }

  public String selectPage(String sql, int begin, int end) throws PersistenceException
  {
    throw new IllegalStateException("not support yet.");
  }

}
